local yinling = fk.CreateSkill {
  name = "rfenghou_3d13j__yinling",
}

yinling:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  if room:getBanner(yinling.name) == nil then
    local ids = {}
    for _, name in ipairs(Fk:getAllCardNames("bt", false, false)) do
      table.insert(ids, room:printCard(name).id)
    end
    room:setBanner(yinling.name, ids)
  end
end)

yinling:addEffect(fk.TurnEnd, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yinling.name) then
      local logic = player.room.logic
      local events = logic:getEventsByRule(GameEvent.UseCard, 2, function(e)
        if e.data and e.data.card and e.data.tos then
          return table.contains(e.data.tos, player)
        end
      end)
      if #events == 1 then
        event:setSkillData(self, "_yinlingCard", Fk:translate(events[1].data.card.name):len())
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cname = 5 - event:getSkillData(self, "_yinlingCard")
    if cname < 1 then return end
    local cards = table.filter(room:getBanner(yinling.name), function (id)
      return Fk:translate(Fk:getCardById(id).name):len() == cname
    end)
    local prompt = "#rfenghou_3d13j__yinling-invoke:::"
    local sober = player.drank == 0
    if sober then
      prompt = "#rfenghou_3d13j__yinling-only:::"
    end
    if #cards > 0 then
      local use = room:askToUseRealCard(player, {
        pattern = cards,
        skill_name = yinling.name,
        prompt = prompt .. cname,
        extra_data = {
          bypass_times = true,
          expand_pile = cards,
        },
        skip = true,
      })
      if use then
        event:setCostData(self, { tos = use.tos, cardName = use.card.name, rfenghou_3d13j__yinlingOnlyTarget = sober })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local tos, is_true = dat.tos, dat.rfenghou_3d13j__yinlingOnlyTarget
    local card = Fk:cloneCard(dat.cardName)
    card.skillName = yinling.name
    room:useCard({
      from = player,
      tos = tos,
      card = card,
      extra_data = {
        rfenghou_3d13j__yinlingOnlyTarget = is_true
      }
    })
  end
})
yinling:addEffect(fk.CardUsing, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.extra_data and data.extra_data.rfenghou_3d13j__yinlingOnlyTarget and #data:getAllTargets() > 1
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = data:getAllTargets()
    if #tos == 0 then return end
    local only = room:askToChoosePlayers(player, {
      targets = tos,
      min_num = 1,
      max_num = 1,
      skill_name = yinling.name,
      prompt = "#rfenghou_3d13j__yinling-target:::" .. data.card:toLogString(),
      cancelable = false
    })[1]
    table.removeOne(tos, only)
    data.nullifiedTargets = tos
  end
})

Fk:loadTranslationTable{
  ["rfenghou_3d13j__yinling"] = "吟翎",
  [":rfenghou_3d13j__yinling"] = "你仅成为过一张牌的目标的回合结束时，你可以视为使用一张与该牌牌名字数和为5的即时牌；若你不处于【酒】状态，以此法使用的牌仅对一个目标生效。",

  ["#rfenghou_3d13j__yinling-invoke"] = "吟翎：你可以视为使用一张牌名字数为 %arg 的即时牌",
  ["#rfenghou_3d13j__yinling-only"] = "吟翎：你可以视为使用一张牌名字数为 %arg 的即时牌，但此牌只对其中一个目标有效",

  ["#rfenghou_3d13j__yinling-target"] = "吟翎：请为 %arg 指定唯一可生效的目标",
}

return yinling